//
//  PictureCache.m
//  Emoticon
//
//  Created by ytkjs2 on 2020/4/3.
//  Copyright © 2020 ytk. All rights reserved.
//

#import "PictureCache.h"

@implementation PictureCache
{
    FMDatabase * _dataBase;
}

+ (PictureCache *)sharedDataBase
{
    static PictureCache * dataBase;
    @synchronized(self){
        if (dataBase == nil) {
            dataBase = [[PictureCache alloc] init];
        }
    }
    return dataBase;
}

-(id)init
{
    if (self = [super init]) {
        [self createdDataBase];
        [self createdTables];
    }
    return self;
}

- (void)createdDataBase
{
    NSLog(@"%@", NSHomeDirectory());
    NSString *document  = NSSearchPathForDirectoriesInDomains(
                                                              NSDocumentDirectory,
                                                              NSUserDomainMask,
                                                              YES)[0];
    NSString * path = [document stringByAppendingPathComponent:@"EmoticonFMDataBase.db"];
    //生成dataBase
    _dataBase = [[FMDatabase alloc] initWithPath:path];
    BOOL ret = [_dataBase open];
    if (!ret) {
        NSLog(@"开启数据库失败，请检查路径...");
    }
}

- (void)createdTables
{
    @synchronized(self){
        NSString * sql = @"CREATE TABLE IF NOT EXISTS  EmoticonCollection (image_id integer PRIMARY KEY,image text);";
        BOOL ret = [_dataBase executeUpdate:sql];
        if (!ret) {
            NSLog(@"创建头像收藏列表错误!");
        }
        
        
        NSString * sql1 = @"CREATE TABLE IF NOT EXISTS  PaperCollection (image_id integer PRIMARY KEY,image text);";
        BOOL ret1 = [_dataBase executeUpdate:sql1];
        if (!ret1) {
            NSLog(@"创建静态壁纸列表错误!");
        }
        
        
        NSString * sql2 = @"CREATE TABLE IF NOT EXISTS  LoverCollection (image_id integer PRIMARY KEY,image text,image1 text,title text);";
        BOOL ret2 = [_dataBase executeUpdate:sql2];
        if (!ret2) {
            NSLog(@"创建情侣壁纸列表错误!");
        }
        
        NSString * sql3 = @"CREATE TABLE IF NOT EXISTS  SearchCollection (_id text PRIMARY KEY,status text,format text,height integer,width integer,small_url text,big_url text,desc text,src text,atime integer,name text,url text,static_url text,groupchat integer);";
        BOOL ret3 = [_dataBase executeUpdate:sql3];
        if (!ret3) {
            NSLog(@"创建搜索表情列表错误!");
        }
        
        NSString * sql4 = @"CREATE TABLE IF NOT EXISTS  SearchBagListCollection (_id text PRIMARY KEY,inbag integer,o_uid text,own integer,uid text,marked integer,height integer,width integer,bag_count integer,big_url text,favor integer,isfavor integer,status text,update_time integer,format text,small_url text,atime integer,desc text,src text,name text,url text,static_url text,groupchat integer,category_id text,display text);";
        BOOL ret4 = [_dataBase executeUpdate:sql4];
        if (!ret4) {
            NSLog(@"创建搜索表情包列表错误!");
        }
    }
}

-(void)insertEmoticonChannerModel:(PicModel * )model
{
    NSString * sql = @"INSERT INTO EmoticonCollection(image_id,image) Values(?,?);";
    
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:sql,@(model.image_id),model.image];
        if (!ret) {
            NSLog(@"插入表情包收藏表单错误");
        }else{
            
        }
    }
}

-(BOOL)isExistEmoticonWithChannelId:(int)image_id
{
    NSString * sql = @"SELECT * FROM EmoticonCollection WHERE image_id = ?;";
    @synchronized(self){
        FMResultSet * set = [_dataBase executeQuery:sql, @(image_id)];
        if ([set next] != NO) {
            return YES;
        }
        return NO;
    }
}

-(void)deleteEmoticonWithChannelId:(int)image_id
{
    NSString * desql = @"DELETE  FROM EmoticonCollection WHERE image_id = ?;";
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:desql,@(image_id)];
        if (!ret) {
            NSLog(@"删除表情包收藏失败");
        }
    }
}

-(NSArray *)selectEmoticon
{
    NSMutableArray  * Muarray=[[NSMutableArray alloc]init];
    NSString * sql = @"SELECT * FROM EmoticonCollection;";
    FMResultSet * set;
    @synchronized(self){
        set = [_dataBase executeQuery:sql];
    }
    while ([set next]) {
        PicModel *model = [[PicModel alloc]init];
        model.image_id = [set intForColumn:@"image_id"];
        model.image = [set stringForColumn:@"image"];
        [Muarray addObject:model];
    }
    return Muarray;
}



-(void)insertPaperChannerModel:(PicModel * )model
{
    NSString * sql = @"INSERT INTO PaperCollection(image_id,image) Values(?,?);";
    
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:sql,@(model.image_id),model.image];
        if (!ret) {
            NSLog(@"插入壁纸收藏表单错误");
        }else{
            
        }
    }
}

-(BOOL)isExistPaperWithChannelId:(int)image_id
{
    NSString * sql = @"SELECT * FROM PaperCollection WHERE image_id = ?;";
    @synchronized(self){
        FMResultSet * set = [_dataBase executeQuery:sql, @(image_id)];
        if ([set next] != NO) {
            return YES;
        }
        return NO;
    }
}

-(void)deletePaperWithChannelId:(int)image_id
{
    NSString * desql = @"DELETE  FROM PaperCollection WHERE image_id = ?;";
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:desql,@(image_id)];
        if (!ret) {
            NSLog(@"删除壁纸收藏失败");
        }
    }
}

-(NSArray *)selectAllPaper
{
    NSMutableArray  * Muarray=[[NSMutableArray alloc]init];
    NSString * sql = @"SELECT * FROM PaperCollection;";
    FMResultSet * set;
    @synchronized(self){
        set = [_dataBase executeQuery:sql];
    }
    while ([set next]) {
        PicModel *model = [[PicModel alloc]init];
        model.image_id = [set intForColumn:@"image_id"];
        model.image = [set stringForColumn:@"image"];
        [Muarray addObject:model];
    }
    return Muarray;
}

-(void)insertLoverChannerModel:(LoverPicModel * )model
{
    NSString * sql = @"INSERT INTO LoverCollection(image_id,image,image1,title) Values(?,?,?,?);";
    
    @synchronized(self){
        NSArray * arr = [NSArray arrayWithArray:model.image];
        BOOL ret = [_dataBase executeUpdate:sql,@(model.image_id),arr[0],arr[1],model.title];
        if (!ret) {
            NSLog(@"插入情侣壁纸收藏表单错误");
        }else{
            
        }
    }
}

-(BOOL)isExistLoverWithChannelId:(int)image_id
{
    NSString * sql = @"SELECT * FROM LoverCollection WHERE image_id = ?;";
    @synchronized(self){
        FMResultSet * set = [_dataBase executeQuery:sql, @(image_id)];
        if ([set next] != NO) {
            return YES;
        }
        return NO;
    }
}

-(void)deleteLoverWithChannelId:(int)image_id
{
    NSString * desql = @"DELETE  FROM LoverCollection WHERE image_id = ?;";
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:desql,@(image_id)];
        if (!ret) {
            NSLog(@"删除情侣壁纸收藏失败");
        }
    }
}

-(NSArray *)selectAllLover
{
    NSMutableArray  * Muarray=[[NSMutableArray alloc]init];
    NSString * sql = @"SELECT * FROM LoverCollection;";
    FMResultSet * set;
    @synchronized(self){
        set = [_dataBase executeQuery:sql];
    }
    while ([set next]) {
        LoverPicModel *model = [[LoverPicModel alloc]init];
        model.image_id = [set intForColumn:@"image_id"];
        model.image = [NSArray arrayWithObjects:[set stringForColumn:@"image"],[set stringForColumn:@"image1"], nil];
        model.title = [set stringForColumn:@"title"];
        [Muarray addObject:model];
    }
    return Muarray;
}

-(void)insertSearchModel:(SearchModel * )model
{
    NSString * sql = @"INSERT INTO SearchCollection(_id,status,format,height,width,small_url,big_url,desc,src,atime,name,url,static_url,groupchat) Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
    
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:sql,model._id,model.status,model.format,@(model.height),@(model.width),model.small_url,model.big_url,model.desc,model.src,@(model.atime),model.name,model.url,model.static_url,@(model.groupchat)];
        if (!ret) {
            NSLog(@"插入表情收藏表单错误");
        }else{
            
        }
    }
}

-(BOOL)isExistSearchWithChannelId:(NSString *)_id
{
    NSString * sql = @"SELECT * FROM SearchCollection WHERE _id = ?;";
    @synchronized(self){
        FMResultSet * set = [_dataBase executeQuery:sql, _id];
        if ([set next] != NO) {
            return YES;
        }
        return NO;
    }
}

-(void)deleteSearchWithChannelId:(NSString *)_id
{
    NSString * desql = @"DELETE  FROM SearchCollection WHERE _id = ?;";
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:desql,_id];
        if (!ret) {
            NSLog(@"删除表情收藏失败");
        }
    }
}

-(NSArray *)selectAllSearch
{
    NSMutableArray  * Muarray=[[NSMutableArray alloc]init];
    NSString * sql = @"SELECT * FROM SearchCollection;";
    FMResultSet * set;
    @synchronized(self){
        set = [_dataBase executeQuery:sql];
    }
    while ([set next]) {
        SearchModel *model = [[SearchModel alloc]init];
        model._id = [set stringForColumn:@"_id"];
        model.status = [set stringForColumn:@"status"];
        model.format = [set stringForColumn:@"format"];
        model.height = [set intForColumn:@"height"];
        model.width = [set intForColumn:@"width"];
        model.small_url = [set stringForColumn:@"small_url"];
        model.big_url = [set stringForColumn:@"big_url"];
        model.desc = [set stringForColumn:@"desc"];
        model.src = [set stringForColumn:@"src"];
        model.atime = [set intForColumn:@"atime"];
        model.name = [set stringForColumn:@"name"];
        model.url = [set stringForColumn:@"url"];
        model.static_url = [set stringForColumn:@"static_url"];
        model.groupchat = [set intForColumn:@"groupchat"];
        [Muarray addObject:model];
    }
    return Muarray;
}

-(void)insertSearchBagListModelModel:(SearchBagListModel * )model
{
    NSString * sql = @"INSERT INTO SearchBagListCollection(_id,inbag,o_uid,own,uid,marked,height,width,bag_count,big_url,favor,isfavor,status,update_time,format,small_url,atime,desc,src,name,url,static_url,groupchat,category_id,display) Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
    
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:sql,model._id,@(model.inbag),model.o_uid,@(model.own),model.uid,@(model.marked),@(model.height),@(model.width),@(model.bag_count),model.big_url,@(model.favor),@(model.isfavor),model.status,@(model.update_time),model.format,model.small_url,@(model.atime),model.desc,model.src,model.name,model.url,model.static_url,@(model.groupchat),model.category_id,model.display];
        if (!ret) {
            NSLog(@"插入表情收藏表单错误");
        }else{
            
        }
    }
}

-(BOOL)isExistSearchBagListWithChannelId:(NSString *)_id
{
    NSString * sql = @"SELECT * FROM SearchBagListCollection WHERE _id = ?;";
    @synchronized(self){
        FMResultSet * set = [_dataBase executeQuery:sql, _id];
        if ([set next] != NO) {
            return YES;
        }
        return NO;
    }
}

-(void)deleteSearchBagWithChannelId:(NSString *)_id
{
    NSString * desql = @"DELETE  FROM SearchBagListCollection WHERE _id = ?;";
    @synchronized(self){
        BOOL ret = [_dataBase executeUpdate:desql,_id];
        if (!ret) {
            NSLog(@"删除表情收藏失败");
        }
    }
}

-(NSArray *)selectAllSearchBag
{
    NSMutableArray  * Muarray=[[NSMutableArray alloc]init];
    NSString * sql = @"SELECT * FROM SearchBagListCollection;";
    FMResultSet * set;
    @synchronized(self){
        set = [_dataBase executeQuery:sql];
    }
    while ([set next]) {
        SearchBagListModel *model = [[SearchBagListModel alloc]init];
        model._id = [set stringForColumn:@"_id"];
        model.inbag = [set intForColumn:@"inbag"];
        model.o_uid = [set stringForColumn:@"o_uid"];
        model.own = [set intForColumn:@"own"];
        model.uid = [set stringForColumn:@"uid"];
        model.marked = [set intForColumn:@"marked"];
        model.height = [set intForColumn:@"height"];
        model.width = [set intForColumn:@"width"];
        model.bag_count = [set intForColumn:@"bag_count"];
        model.big_url = [set stringForColumn:@"big_url"];
        model.favor = [set intForColumn:@"favor"];
        model.isfavor = [set intForColumn:@"isfavor"];
        model.status = [set stringForColumn:@"status"];
        model.update_time = [set intForColumn:@"update_time"];
        model.format = [set stringForColumn:@"format"];
        model.small_url = [set stringForColumn:@"small_url"];
        model.atime = [set intForColumn:@"atime"];
        model.desc = [set stringForColumn:@"desc"];
        model.src = [set stringForColumn:@"src"];
        model.big_url = [set stringForColumn:@"big_url"];
        model.desc = [set stringForColumn:@"desc"];
        model.src = [set stringForColumn:@"src"];
        model.name = [set stringForColumn:@"name"];
        model.url = [set stringForColumn:@"url"];
        model.static_url = [set stringForColumn:@"static_url"];
        model.groupchat = [set intForColumn:@"groupchat"];
        model.category_id = [NSArray arrayWithObject:[set stringForColumn:@"category_id"]];
        model.display = [NSArray arrayWithObject:[set stringForColumn:@"display"]];
        [Muarray addObject:model];
    }
    return Muarray;
}
@end
